/**
 * @file    Expression classname is undefined on line 2, column 15 in Templates/Classes/Class.java.
 * @author  Haseeb Yousaf
 * @see     LICENSE (MIT style license file)
 * @version 1.0
 * @date    Nov 10, 2011 12:13:59 PM
 */
package carrello.model.entities;

import carrello.model.association.Invoice;
import carrello.util.EntityManagerUtil;
import java.util.List;
import java.util.Set;
import javax.persistence.*;

@NamedQueries({
	@NamedQuery(
	    name = "getUserByUserName",
	    query = "from User u where u.username = :s"
	    ),
        @NamedQuery(
            name = "getAllUsers",
            query = "from User"
            )
        })

@Table
@Entity(name = "User")
public class User {

    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name = "user_id")
    private int userId;
    
    private String username;
    private String firstName;
    private String lastName;
    private String passwordHash;
    private String email;
    private String address;
    private String role;
    @OneToMany(fetch = FetchType.EAGER, mappedBy = "user")
    private Set<Invoice> invoice;

    public User() {
    }

    public User( String username, String firstName, String lastName, String passwordHash, String email, String address, String role) {
//        this.userId = userId;
        this.username = username;
        this.firstName = firstName;
        this.lastName = lastName;
        this.passwordHash = passwordHash;
        this.email = email;
        this.address = address;
        this.role = role;
    }

    public User( String username, String firstName, String lastName, String passwordHash, String email, String address, String role, Set<Invoice> invoice) {
        this.username = username;
        this.firstName = firstName;
        this.lastName = lastName;
        this.passwordHash = passwordHash;
        this.email = email;
        this.address = address;
        this.role = role;
        this.invoice = invoice;
    }

    public int getUserId() {
        return userId;
    }
 
    
    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getPasswordHash() {
        return passwordHash;
    }

    public void setPasswordHash(String passwordHash) {
        this.passwordHash = passwordHash;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Set<Invoice> getInvoice() {
        return invoice;
    }

    public void setInvoice(Set<Invoice> invoice) {
        this.invoice = invoice;
    }
 
    public static List<User> getAllUsers() {
        List<User> u = EntityManagerUtil.getEntityManager().getQuery("getAllUsers").list();
        return u;
    }   
    public static User getUserByUserName(String username)
    {
       try
       {
       Object i = EntityManagerUtil.getEntityManager().getQuery("getUserByUserName")
                                  .setParameter("s", username).uniqueResult();
                                  
       if ( i instanceof User) {
           return (User)i;
       }
       return null;
       }
       catch (Exception ex) {
           return null;
       }
    }
    
//    public static void main(String [] args)
//    {
//        getAllUsers ();
//        User cat = getUserByUserName("admin");
//        System.out.println(cat.getEmail());
//    }
}
